package com.gewu.pm.ui.adapter;

import android.content.Context;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;

import com.gewu.pm.R;
import com.gewu.pm.app.AppAdapter;
import com.gewu.pm.bean.AttendanceRecordBean;

/**
 * 我的打卡记录适配器
 */
public final class MyAttendanceAdapter extends AppAdapter<AttendanceRecordBean> {

    private OnItemActionListener mListener;

    public MyAttendanceAdapter(Context context) {
        super(context);
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new ViewHolder();
    }

    public void setOnItemActionListener(OnItemActionListener listener) {
        mListener = listener;
    }

    private final class ViewHolder extends AppAdapter<AttendanceRecordBean>.ViewHolder {

        private final TextView tvDateGroup;
        private final LinearLayout llSignIn;
        private final View vSignInLine;
        private final TextView tvSignInTime;
        private final TextView tvSignInStatus;
        private final TextView tvFillDescription;
        private final TextView tvViewPhoto;
        
        private final LinearLayout llSignOut;
        private final View vSignOutLine;
        private final TextView tvSignOutTime;
        private final TextView tvSignOutStatus;
        private final LinearLayout llSignOutActions;
        private final TextView tvFillDescriptionOut;
        private final TextView tvViewPhotoOut;

        ViewHolder() {
            super(R.layout.my_attendance_item);
            tvDateGroup = findViewById(R.id.tv_date_group);
            llSignIn = findViewById(R.id.ll_sign_in);
            vSignInLine = findViewById(R.id.v_sign_in_line);
            tvSignInTime = findViewById(R.id.tv_sign_in_time);
            tvSignInStatus = findViewById(R.id.tv_sign_in_status);
            tvFillDescription = findViewById(R.id.tv_fill_description);
            tvViewPhoto = findViewById(R.id.tv_view_photo);
            
            llSignOut = findViewById(R.id.ll_sign_out);
            vSignOutLine = findViewById(R.id.v_sign_out_line);
            tvSignOutTime = findViewById(R.id.tv_sign_out_time);
            tvSignOutStatus = findViewById(R.id.tv_sign_out_status);
            llSignOutActions = findViewById(R.id.ll_sign_out_actions);
            tvFillDescriptionOut = findViewById(R.id.tv_fill_description_out);
            tvViewPhotoOut = findViewById(R.id.tv_view_photo_out);
        }

        @Override
        public void onBindView(int position) {
            AttendanceRecordBean bean = getItem(position);
            
            // 日期分组（只在第一条或日期变化时显示）
            if (position == 0 || !bean.getDate().equals(getItem(position - 1).getDate())) {
                tvDateGroup.setVisibility(View.VISIBLE);
                tvDateGroup.setText(bean.getDate() + " " + getWeekDay(bean.getDate()));
            } else {
                tvDateGroup.setVisibility(View.GONE);
            }
            
            // 签到信息
            if (!TextUtils.isEmpty(bean.getSignInTime())) {
                tvSignInTime.setText(bean.getSignInTime());
            } else {
                tvSignInTime.setText("");
            }
            
            // 签到状态
            int checkStatus = bean.getCheckStatus();
            if (checkStatus == 0) {
                // 未打卡
                tvSignInTime.setText(getString(R.string.not_clocked_in));
                tvSignInTime.setTextColor(getColor(R.color.red_app));
                tvSignInStatus.setVisibility(View.GONE);
            } else if (checkStatus == 1) {
                // 正常签到
                tvSignInTime.setTextColor(getColor(R.color.cb3));
                tvSignInStatus.setVisibility(View.GONE);
            } else if (checkStatus == 2) {
                // 迟到
                tvSignInTime.setTextColor(getColor(R.color.cb3));
                tvSignInStatus.setVisibility(View.VISIBLE);
                tvSignInStatus.setText(R.string.late);
            }
            
            // 签退信息
            if (!TextUtils.isEmpty(bean.getSignOutTime())) {
                tvSignOutTime.setText(bean.getSignOutTime());
            } else {
                tvSignOutTime.setText("");
            }
            
            // 签退状态
            int signOffStatus = bean.getSignOffStatus();
            if (signOffStatus == 0) {
                // 未打卡
                tvSignOutTime.setText(getString(R.string.not_clocked_in));
                tvSignOutTime.setTextColor(getColor(R.color.red_app));
                tvSignOutStatus.setVisibility(View.GONE);
                llSignOutActions.setVisibility(View.GONE);
            } else if (signOffStatus == 1) {
                // 正常签退
                tvSignOutTime.setTextColor(getColor(R.color.cb3));
                tvSignOutStatus.setVisibility(View.GONE);
                llSignOutActions.setVisibility(View.VISIBLE);
            } else if (signOffStatus == 2) {
                // 早退
                tvSignOutTime.setTextColor(getColor(R.color.cb3));
                tvSignOutStatus.setVisibility(View.VISIBLE);
                tvSignOutStatus.setText(R.string.early_leave);
                llSignOutActions.setVisibility(View.VISIBLE);
            }
            
            // 时间轴竖线（最后一条记录不显示签退的竖线）
            vSignInLine.setVisibility(View.VISIBLE);
            vSignOutLine.setVisibility(position == getItemCount() - 1 ? View.GONE : View.VISIBLE);
            
            // ✅ BaseAdapter 会自动处理 itemView 的点击事件
            // 子View的点击事件通过自定义接口处理
            tvFillDescription.setOnClickListener(v -> {
                if (mListener != null) {
                    mListener.onFillDescription(position, bean, true);
                }
            });
            
            tvViewPhoto.setOnClickListener(v -> {
                if (mListener != null) {
                    mListener.onViewPhoto(position, bean, true);
                }
            });
            
            tvFillDescriptionOut.setOnClickListener(v -> {
                if (mListener != null) {
                    mListener.onFillDescription(position, bean, false);
                }
            });
            
            tvViewPhotoOut.setOnClickListener(v -> {
                if (mListener != null) {
                    mListener.onViewPhoto(position, bean, false);
                }
            });
        }
        
        /**
         * 获取星期几
         */
        private String getWeekDay(String date) {
            // 简单实现，实际应该根据日期计算
            return "星期三";
        }
    }

    /**
     * 子View点击事件接口（不包括Item整体点击）
     */
    public interface OnItemActionListener {
        /**
         * 填写说明
         * @param isSignIn true=签到，false=签退
         */
        void onFillDescription(int position, AttendanceRecordBean bean, boolean isSignIn);

        /**
         * 查看图片
         * @param isSignIn true=签到，false=签退
         */
        void onViewPhoto(int position, AttendanceRecordBean bean, boolean isSignIn);
    }
}
